#include "stdafx.h"

#include "Controller.h"
#include "GUIController.h"
#include "PlayerAvatar.h"
#include "InGameGUIController.h"
#include <string>
#include <CEGUI/CEGUI.h>
#include <CEGUI/RendererModules/Ogre/CEGUIOgreRenderer.h>

using namespace std;

bool m_GUIMade = false;

CEGUI::MouseButton convertButton(OIS::MouseButtonID buttonID);

InGameGUIController::InGameGUIController (Model *model, Ogre::RenderWindow* window, string scheme, string layout) : GUIController(model, window, scheme) {
	GUIController::setGUIsheet(layout);

	// Remove the mouse cursor
	CEGUI::System::getSingleton().setDefaultMouseCursor(NULL);

	InGameGUIController::makeGUILayout();
	m_playerAvatar = NULL;
}

InGameGUIController::~InGameGUIController () {
	if(m_sheet) {
		CEGUI::WindowManager::getSingleton().destroyWindow(m_sheet);
	}
}

void InGameGUIController::setPlayerAvatar(PlayerAvatar* avatar) {
	m_playerAvatar = avatar;
}
	
bool InGameGUIController::update(float deltaTime) {
	if (m_GUIMade && m_playerAvatar) {
		unsigned playerHealth = m_playerAvatar->getHealth();
		int playerHealthInt = int(playerHealth);

		// Using the new string conversion tool in C++ 2011
		string playerHealthString = to_string((long long)playerHealthInt);

		m_timerSec += deltaTime;
		if (m_timerSec>=1) {
			m_timerInt -= 1;
			m_timerSec = 0;
		}	
		m_timerString = to_string((long long)m_timerInt);

		m_healthCounter->setText("Health: " +playerHealthString);
		m_timeCounter->setText("Time: " +m_timerString);
	}

	m_GUIMade = true;
	return GUIController::update(deltaTime);
}

void InGameGUIController::makeGUILayout() {
	
	CEGUI::WindowManager &wmgr = CEGUI::WindowManager::getSingleton();
	CEGUI::Window *m_sheet = wmgr.createWindow("DefaultWindow", "InGameGUI/Sheet");

	// Create the health counter in the bottom left
	m_healthCounter = wmgr.createWindow("OgreTray/StaticText", "InGameGUI/healthcounter");
	m_healthCounter->setText("Health: 100");
	m_healthCounter->setSize(CEGUI::UVector2(CEGUI::UDim(0.15f, 0), CEGUI::UDim(0.10f, 0)));
	m_healthCounter->setHorizontalAlignment(CEGUI::HA_LEFT);
	m_healthCounter->setVerticalAlignment(CEGUI::VA_BOTTOM);
	m_healthCounter->setProperty("HorzFormatting", "HorzCentred"); 
	m_sheet->addChildWindow(m_healthCounter);

	// Create the time counter in the middle top
	m_timeCounter = wmgr.createWindow("OgreTray/StaticText", "InGameGUI/TimeCounter");
	m_timerInt = 120;
	m_timerSec = 0;
	m_timerString = to_string((long long)m_timerInt);
	m_timeCounter->setText("Time: " + m_timerString);
	m_timeCounter->setSize(CEGUI::UVector2(CEGUI::UDim(0.15f, 0), CEGUI::UDim(0.10f, 0)));
	m_timeCounter->setHorizontalAlignment(CEGUI::HA_CENTRE);
	m_timeCounter->setVerticalAlignment(CEGUI::VA_TOP);
	m_timeCounter->setProperty("HorzFormatting", "HorzCentred"); 
	m_sheet->addChildWindow(m_timeCounter);

	CEGUI::System::getSingleton().setGUISheet(m_sheet);
}
